package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.Restaurant;
import com.wm.bean.RestaurantScriptoriumCon;
import com.wm.bean.Scriptorium;
import com.wm.comm.Constant;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.RestaurantScriptoriumConService;
import com.wm.service.RestaurantService;
import com.wm.service.ScriptoriumService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;
import com.wm.web.ui.OptionsLong;

public class AdminRestaurantScriptoriumCon extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminRestaurantScriptoriumCon.class);
	
	private RestaurantScriptoriumConService restaurantScriptoriumConService;
	private ScriptoriumService scriptoriumService;
	private RestaurantService restaurantService;

	public RestaurantScriptoriumConService getRestaurantScriptoriumConService() {
		return restaurantScriptoriumConService;
	}

	public void setRestaurantScriptoriumConService(
			RestaurantScriptoriumConService restaurantScriptoriumConService) {
		this.restaurantScriptoriumConService = restaurantScriptoriumConService;
	}

	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}
	
	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}
	
	private List isShowList = new ArrayList();

	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void initValues(){
		setIsShowListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		this.restaurant = this.getRestaurantService().findRestaurantById(this.resId);
	}


	private long id;					//主键ID
	private long resId = -1;			//饭店ID
	private long scrId = -1;			//写字间ID
	private long cityId = -1;			//城市ID
	private long areaId = -1;			//地区ID
	private String conMemo;				//备注
	private int isShow = -1;			//是否显示
	private int orderNum;				//排序字段
	private String scrName;				//写字间名称
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getResId() {
		return resId;
	}

	public void setResId(long resId) {
		this.resId = resId;
	}

	public long getScrId() {
		return scrId;
	}

	public void setScrId(long scrId) {
		this.scrId = scrId;
	}

	public long getCityId() {
		return cityId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public long getAreaId() {
		return areaId;
	}

	public void setAreaId(long areaId) {
		this.areaId = areaId;
	}

	public String getConMemo() {
		return conMemo;
	}

	public void setConMemo(String conMemo) {
		this.conMemo = conMemo;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	public String getScrName() {
		return scrName;
	}

	public void setScrName(String scrName) {
		this.scrName = scrName;
	}
	
	private Restaurant restaurant;
	private List conList;	
	
	public Restaurant getRestaurant() {
		return restaurant;
	}

	public void setRestaurant(Restaurant restaurant) {
		this.restaurant = restaurant;
	}
	
	public List getConList() {
		return conList;
	}

	public void setConList(List conList) {
		this.conList = conList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		this.setConList(this.getRestaurantScriptoriumConService().findRestaurantScriptoriumConsByResIdIsShow(this.resId,this.isShow,"id", 1));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.scrId);
		//添加
		RestaurantScriptoriumCon restaurantScriptoriumCon = new RestaurantScriptoriumCon();
		restaurantScriptoriumCon.setResId(this.resId);
		restaurantScriptoriumCon.setScrId(this.scrId);
		restaurantScriptoriumCon.setCityId(scriptorium.getCityId());
		restaurantScriptoriumCon.setAreaId(scriptorium.getAreaId());
		restaurantScriptoriumCon.setConMemo(this.conMemo);
		restaurantScriptoriumCon.setIsShow(this.isShow);
		restaurantScriptoriumCon.setOrderNum(this.orderNum);
		restaurantScriptoriumCon.setOrderTime(new Date());
		restaurantScriptoriumCon.setAddTime(new Date());
		try {
			this.getRestaurantScriptoriumConService().saveRestaurantScriptoriumCon(restaurantScriptoriumCon);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		RestaurantScriptoriumCon restaurantScriptoriumCon = this.getRestaurantScriptoriumConService().findRestaurantScriptoriumConById(this.id);
		this.setResId(restaurantScriptoriumCon.getResId());
		this.setScrId(restaurantScriptoriumCon.getScrId());
		this.setCityId(restaurantScriptoriumCon.getCityId());
		this.setAreaId(restaurantScriptoriumCon.getAreaId());
		this.setConMemo(restaurantScriptoriumCon.getConMemo());
		this.setIsShow(restaurantScriptoriumCon.getIsShow());
		this.setOrderNum(restaurantScriptoriumCon.getOrderNum());
		Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(restaurantScriptoriumCon.getResId());
		this.setScrName(scriptorium.getScrName());
		return INPUT;
	}
	
	public String doupdate(){
		Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.scrId);
		//添加
		RestaurantScriptoriumCon restaurantScriptoriumCon = this.getRestaurantScriptoriumConService().findRestaurantScriptoriumConById(this.id);
		restaurantScriptoriumCon.setResId(this.resId);
		restaurantScriptoriumCon.setScrId(this.scrId);
		restaurantScriptoriumCon.setCityId(scriptorium.getCityId());
		restaurantScriptoriumCon.setAreaId(scriptorium.getAreaId());
		restaurantScriptoriumCon.setConMemo(this.conMemo);
		restaurantScriptoriumCon.setIsShow(this.isShow);
		restaurantScriptoriumCon.setOrderNum(this.orderNum);
		restaurantScriptoriumCon.setOrderTime(new Date());
		restaurantScriptoriumCon.setAddTime(new Date());
		try {
			this.getRestaurantScriptoriumConService().updateRestaurantScriptoriumCon(restaurantScriptoriumCon);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}

	public String orders(){
		RestaurantScriptoriumCon restaurantScriptoriumCon = this.getRestaurantScriptoriumConService().findRestaurantScriptoriumConById(this.id);
		restaurantScriptoriumCon.setOrderTime(new Date());
		try {
			this.getRestaurantScriptoriumConService().updateRestaurantScriptoriumCon(restaurantScriptoriumCon);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getRestaurantScriptoriumConService().removeRestaurantScriptoriumConById(this.id);
		return "index";
	}


}
